package topin.facecat.FaceCat.UI

import facecat.topin.core.FCInvokeEventCallBack
import facecat.topin.core.RefObject
import facecat.topin.service.FCClientService
import facecat.topin.service.FCListenerMessageCallBack
import facecat.topin.service.FCMessage
import facecat.topin.tab.FCTabView
import topin.facecat.FaceCat.UI.DataCenter.startService
import topin.facecat.FaceCat.Service.HistoryServiceClient
import topin.facecat.FaceCat.Service.LatestServiceClient
import topin.facecat.FaceCat.Service.Security
import topin.facecat.FaceCat.Service.SecurityData
import topin.facecat.FaceCat.Service.SecurityLatestData
import topin.facecat.FaceCat.Service.StockService.ConvertXDR
import topin.facecat.FaceCat.Service.StockService.getHistoryHalfYearDatas
import topin.facecat.FaceCat.Service.StockService.getHistoryMonthDatas
import topin.facecat.FaceCat.Service.StockService.getHistorySeasonDatas
import topin.facecat.FaceCat.Service.StockService.getHistoryWeekDatas
import topin.facecat.FaceCat.Service.StockService.getHistoryYearDatas
import topin.facecat.FaceCat.Service.StockService.multiMinuteSecurityDatas

class MainFrame : UIXmlEx(), FCListenerMessageCallBack, FCInvokeEventCallBack, Runnable {
    var m_isConnected = false

    /*
     * 运行线程
     */
    override fun run() {
        startService()
        DataCenter.startConnect()
        m_isConnected = true
    }

    var m_tabFunc: FCTabView? = null

     fun loadXml(xmlPath: String) {
        super.loadXml(xmlPath, null)
        m_tabFunc = findView("tabFunc") as FCTabView
        m_tabFunc!!.addEvent(this, "oninvoke", this)
        val t = Thread(this)
        t.start()
        while (!m_isConnected) {
            try {
                Thread.sleep(1000)
            } catch (ex: Exception) {
            }
        }
        startConnect()
    }

    fun startConnect() {
        val securityList = native.findView("gridStocks") as SecurityList
        securityList.firstSearchCode()
        searchCode("600000.SH")
    }

    var m_requestID1 = -1
    var m_requestID2 = -1
    var m_latestData: SecurityLatestData? = null
    var m_lastCode = ""

    init {
        m_mainFrame = this
    }

    fun searchCode(code: String) {
        if (m_requestID1 != -1) {
            DataCenter.m_latestServiceClient!!.removeListener(m_requestID1)
            DataCenter.m_latestServiceClient!!.unSubCodes(m_requestID1)
        }
        val oldReqID = m_requestID2
        if (m_requestID2 != -1) {
            DataCenter.m_historyServiceClient!!.removeListener(m_requestID2)
        }
        m_requestID1 = FCClientService.getRequestID()
        m_requestID2 = FCClientService.getRequestID()
        for (i in m_charts.indices) {
            val myChart = m_charts[i]
            myChart.m_code = code
            myChart.m_subscription = myChart.m_chart!!.subScription
            myChart.m_chart!!.m_floatStrs.clear()
            if (myChart.m_cycle == 0) {
                myChart.m_chart!!.m_subComboBox!!.isVisible = false
            } else {
                val security = Security()
                if (DataCenter.m_securityService!!.getSecurityByCode(myChart.m_code, security)) {
                    if (security.m_type > 0) {
                        myChart.m_chart!!.m_subComboBox!!.isVisible = true
                    } else {
                        myChart.m_chart!!.m_subComboBox!!.isVisible = false
                    }
                } else {
                    myChart.m_chart!!.m_subComboBox!!.isVisible = false
                }
            }
        }
        m_latestData = null
        DataCenter.m_latestServiceClient!!.addListener(m_requestID1, this, null)
        DataCenter.m_historyServiceClient!!.addListener(m_requestID2, this, null)
        DataCenter.m_latestServiceClient!!.subCodes(m_requestID1, code)
        DataCenter.m_historyServiceClient!!.reqData2(m_requestID2, code, oldReqID)
        m_lastCode = code
    }

    override fun callListenerMessageEvent(sender: Any, message: FCMessage, invoke: Any) {
        m_tabFunc!!.invoke(message)
    }

    fun bindData(message: FCMessage?) {
        if (message!!.m_functionID == HistoryServiceClient.FUNCTIONID_GETDATA2) {
            if (message.m_requestID == m_requestID2) {
                var code = ""
                val dayDatas = ArrayList<SecurityData>()
                val minuteDatas = ArrayList<SecurityData>()
                val trendDatas = ArrayList<SecurityData>()
                val Refcode = RefObject(code)
                HistoryServiceClient.getDatas2(
                    Refcode,
                    dayDatas,
                    minuteDatas,
                    trendDatas,
                    message.m_body,
                    message.m_bodyLength
                )
                code = Refcode.argvalue
                var hasAfter = false
                for (i in m_charts.indices) {
                    val myChart = m_charts[i]
                    if (myChart.m_cycle > 0) {
                        if (myChart.m_subscription == 2) {
                            hasAfter = true
                        }
                    }
                }
                val beforeDatas = ArrayList<SecurityData>()
                val afterDatas = ArrayList<SecurityData>()
                for (i in dayDatas.indices) {
                    val data = dayDatas[i]
                    val fData = SecurityData()
                    fData.copy(data!!)
                    beforeDatas.add(fData)
                    if (hasAfter) {
                        val bData = SecurityData()
                        bData.copy(data)
                        afterDatas.add(bData)
                    }
                }
                val beforeDatas2 = ArrayList<SecurityData>()
                val afterDatas2 = ArrayList<SecurityData>()
                for (i in minuteDatas.indices) {
                    val data = minuteDatas[i]
                    val fData = SecurityData()
                    fData.copy(data!!)
                    beforeDatas2.add(fData)
                    if (hasAfter) {
                        val bData = SecurityData()
                        bData.copy(data)
                        afterDatas2.add(bData)
                    }
                }
                ConvertXDR(code, 1, beforeDatas)
                if (hasAfter) {
                    ConvertXDR(code, 2, afterDatas)
                }
                ConvertXDR(code, 1, beforeDatas2)
                if (hasAfter) {
                    ConvertXDR(code, 2, afterDatas2)
                }
                for (i in m_charts.indices) {
                    val myChart = m_charts[i]
                    if (myChart.m_cycle == 0) {
                        myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, trendDatas, false)
                    } else if (myChart.m_cycle > 0 && myChart.m_cycle < 1440) {
                        var sDatas: ArrayList<SecurityData>? = null
                        sDatas = if (myChart.m_subscription == 1) {
                            beforeDatas2
                        } else if (myChart.m_subscription == 2) {
                            afterDatas2
                        } else {
                            minuteDatas
                        }
                        if (myChart.m_cycle > 1) {
                            val newDatas = ArrayList<SecurityData>()
                            multiMinuteSecurityDatas(newDatas, sDatas, myChart.m_cycle)
                            myChart.bindHistoryDatas(
                                myChart.m_code,
                                myChart.m_cycle,
                                newDatas,
                                false
                            )
                        } else {
                            myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, sDatas, false)
                        }
                    } else if (myChart.m_cycle >= 1440) {
                        var sDatas: ArrayList<SecurityData>? = null
                        sDatas = if (myChart.m_subscription == 1) {
                            beforeDatas
                        } else if (myChart.m_subscription == 2) {
                            afterDatas
                        } else {
                            dayDatas
                        }
                        if (myChart.m_cycle == 10080) {
                            val newDatas = ArrayList<SecurityData>()
                            getHistoryWeekDatas(newDatas, sDatas)
                            myChart.bindHistoryDatas(
                                myChart.m_code,
                                myChart.m_cycle,
                                newDatas,
                                false
                            )
                        } else if (myChart.m_cycle == 43200) {
                            val newDatas = ArrayList<SecurityData>()
                            getHistoryMonthDatas(newDatas, sDatas)
                            myChart.bindHistoryDatas(
                                myChart.m_code,
                                myChart.m_cycle,
                                newDatas,
                                false
                            )
                        } else if (myChart.m_cycle == 129600) {
                            val newDatas = ArrayList<SecurityData>()
                            getHistorySeasonDatas(newDatas, sDatas)
                            myChart.bindHistoryDatas(
                                myChart.m_code,
                                myChart.m_cycle,
                                newDatas,
                                false
                            )
                        } else if (myChart.m_cycle == 259200) {
                            val newDatas = ArrayList<SecurityData>()
                            getHistoryHalfYearDatas(newDatas, sDatas)
                            myChart.bindHistoryDatas(
                                myChart.m_code,
                                myChart.m_cycle,
                                newDatas,
                                false
                            )
                        } else if (myChart.m_cycle == 518400) {
                            val newDatas = ArrayList<SecurityData>()
                            getHistoryYearDatas(newDatas, sDatas)
                            myChart.bindHistoryDatas(
                                myChart.m_code,
                                myChart.m_cycle,
                                newDatas,
                                false
                            )
                        } else {
                            myChart.bindHistoryDatas(myChart.m_code, myChart.m_cycle, sDatas, false)
                        }
                    }
                }
                dayDatas.clear()
                minuteDatas.clear()
                trendDatas.clear()
                beforeDatas.clear()
                afterDatas.clear()
                beforeDatas2.clear()
                afterDatas2.clear()
                native.invalidate()
            }
        } else if (message.m_functionID == LatestServiceClient.FUNCTION_NEWDATA) {
            if (message.m_requestID == m_requestID1) {
                val latestData =
                    LatestServiceClient.getLatestData(message.m_body, message.m_bodyLength)
                //                LatestDiv latestDiv = findView("divLatest") as LatestDiv;
                val parent = findView("divLatest")
                latestData!!.m_requestID = m_requestID1
                if (m_latestData != null && m_latestData!!.m_requestID == m_requestID1) {
                    for (i in m_charts.indices) {
                        val myChart = m_charts[i]
                        if (myChart.m_chart!!.m_datas!!.size > 0) {
                            myChart.newData(latestData, latestData.m_requestID, true)
                        }
                    }
                }
                m_latestData = latestData
                (parent as? LatestDiv)!!.bindData(latestData, true)
            }
        }
    }

    override fun callInvokeEvent(eventName: String, sender: Any, args: Any, invoke: Any) {
        if (args != null) {
            //            FCMessage message = args as FCMessage;
            bindData(args as? FCMessage)
        }
    }

    companion object {
        lateinit var m_mainFrame: MainFrame
        var m_charts = ArrayList<MyChart>()
    }
}

